package com.haixiaoke.business;

import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.core.page.TableDataInfo;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockFingerprint;
import com.haixiaoke.service.ISmartLockFingerprintService;
import com.haixiaoke.service.ISmartLockService;
import com.haixiaoke.smartLock.tianwanghulian.SmartLockTwhlEventHandleService;
import com.haixiaoke.smartLock.yaluete.SmartLockYLTEventService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 智能锁指纹授权信息Controller
 *
 * @author xiaolong
 * @date 2023-05-27
 */
@RestController
@RequestMapping("/business/fingerprint")
public class SmartLockFingerprintController extends BaseController {
    @Autowired
    private ISmartLockFingerprintService smartLockFingerprintService;
    @Autowired
    private ISmartLockService smartLockService;
    @Autowired
    private SmartLockTwhlEventHandleService smartLockTwhlEventHandleService;
    @Autowired
    private SmartLockYLTEventService smartLockYLTEventService;

    /**
     * 查询智能锁指纹授权信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SmartLockFingerprint smartLockFingerprint) {
        startPage();
        List<SmartLockFingerprint> list = smartLockFingerprintService.selectSmartLockFingerprintList(smartLockFingerprint);
        return getDataTable(list);
    }

    /**
     * 导出智能锁指纹授权信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmartLockFingerprint smartLockFingerprint) {
        List<SmartLockFingerprint> list = smartLockFingerprintService.selectSmartLockFingerprintList(smartLockFingerprint);
        ExcelUtil<SmartLockFingerprint> util = new ExcelUtil<SmartLockFingerprint>(SmartLockFingerprint.class);
        util.exportExcel(response, list, "智能锁指纹授权信息数据");
    }

    /**
     * 获取智能锁指纹授权信息详细信息
     */
    @GetMapping(value = "/{fingerprintId}")
    public AjaxResult getInfo(@PathVariable("fingerprintId") Long fingerprintId) {
        return success(smartLockFingerprintService.selectSmartLockFingerprintByFingerprintId(fingerprintId));
    }

    /**
     * 新增智能锁指纹授权信息和授权指纹
     */
    @PostMapping
    public AjaxResult add(@RequestBody SmartLockFingerprint smartLockFingerprint) {
        SmartLock smartLock = smartLockService.selectBySn(smartLockFingerprint.getSn());
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYLTEventService.insertSmartLockFingerprint(smartLockFingerprint));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockTwhlEventHandleService.insertSmartLockFingerprint(smartLockFingerprint));
        } else {
            return AjaxResult.error("该设备无指纹授权功能");
        }
    }

    /**
     * 修改智能锁指纹授权信息和下发指纹信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SmartLockFingerprint smartLockFingerprint) {
        SmartLock smartLock = smartLockService.selectBySn(smartLockFingerprint.getSn());
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return error("三代智能锁");
        } else {
            return toAjax(smartLockTwhlEventHandleService.updateSmartLockFingerprint(smartLockFingerprint));
        }
    }

    /**
     * 批量删除智能锁指纹授权信息和删除指纹授权信息
     */
    @DeleteMapping("/{fingerprintIds}")
    @Transactional
    public AjaxResult remove(@PathVariable Long[] fingerprintIds) {
        Integer count = 0;
        for (Long fingerprintId : fingerprintIds) {
            SmartLockFingerprint smartLockFingerprint = smartLockFingerprintService.selectSmartLockFingerprintByFingerprintId(fingerprintId);
            SmartLock smartLock = smartLockService.selectBySn(smartLockFingerprint.getSn());
            if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
                count += smartLockYLTEventService.deleteSmartLockFingerprint(smartLockFingerprint);
            } else {
                count += smartLockTwhlEventHandleService.deleteSmartLockFingerprintByFingerprintIds(fingerprintId);
            }
        }
        if (fingerprintIds.length != count) {
            throw new ServiceException("删除授权异常");
        } else {
            return toAjax(1);
        }
    }


}
